<!-- 导入管理员 -->
<template>
  <div>
    <a-modal :maskClosable="false" centered :width="600" v-model="visible" :footer="null">
      <template slot="title">
        <div class="disFlxJS _mr-50">
          <div>{{title}}<span class="fz-10 _pl-10" style="color:#666;">(可把文件拖入指定区域,完成上传)</span></div>
          <a-button class="fz-12" size="small" type="primary" @click="handleDownloadTemp">下载导入模板</a-button>
        </div>
      </template>
      <a-spin :spinning="loading">
        <div class="hoverBox">
          <UploadFile @upload="uploadSuccess">
            <div style="width: 550px;height: 170px;" class="disFlxCC">
              <a-icon type="upload" class="fz-30 text" />
              <div class="fw-6 _pt-10 fz-15 text">上传导入文件</div>
              <div class="fz-12 _pt-10 text">导入需先下载导入模板,且按模板中说明及要求进行规范填写</div>
              <div class="fz-12 _pt-5 text">上传按模板规范填写后的文件,系统会自动进行批量{{title}}</div>
            </div>
          </UploadFile>
        </div>
      </a-spin>
    </a-modal>
  </div>
</template>

<script>
import UploadFile from '@/components/UploadFile/excel.vue'
import { userExportTemplate, userImportExcel } from '@/api/system/system'
import { mimeMap, readAsText, resolveBlob } from '@/utils/download'

export default {
  props: {
    title: {
      type: String,
      default: '导入管理员'
    },
  },
  components: {
    UploadFile,
  },
  data() {
    return {
      visible: false,
      loading: false,
    }
  },
  methods: {
    moduleShow() {
      this.visible = true
    },
    handleCancel() {
      this.visible = false
    },
    async handleDownloadTemp() {
      await userExportTemplate()
    },
    async uploadSuccess(form) {
      this.loading = true
      let res = await userImportExcel(form)
      if (res.data && res.data.type === 'application/json') {
        readAsText(res.data).then((result) => {
          if (result.code === 0) {
            this.visible = false
            this.$message.success('导入成功')
            this.$emit('success')
          } else {
            this.$message.error(result.msg)
          }
        })
      } else {
        this.$message.error('导入数据存在异常，请查看系统下载的异常文档')
        this.visible = false
        this.$emit('success')
        resolveBlob(res, mimeMap.xlsx)
      }
      this.loading = false
    },
  },
}
</script>
